<script lang="ts" setup name="XtxConfirm">
import { onMounted, PropType, ref } from 'vue'
import XtxButton from '../button/index.vue'
defineProps({
  title: {
    type: String,
    default: '默认标题',
  },
  text: {
    type: String,
    default: '默认提示内容',
  },
  cancel: {
    type: Function as PropType<() => void>,
    required: true,
  },
  confirm: {
    type: Function as PropType<() => void>,
    required: true,
  },
})
const show = ref(false)
onMounted(() => {
  // 单独开辟一个新的宏任务, 让设置为true的操作在下一个宏任务中执行
  setTimeout(() => {
    show.value = true
  }, 0)
})
</script>
<template>
  <!-- 不使用Message组件的过渡效果, 涉及到两个元素 各自设置过渡样式
    半透明的背景  淡入
    白色的容器    上=>下 滑入

    两个元素设置为不显示 通过类名的方式控制即可
  -->
  <div class="xtx-confirm" @click.self="cancel" :class="{ show: show }">
    <div class="wrapper" :class="{ show }">
      <div class="header">
        <h3>{{ title }}</h3>
        <a
          href="JavaScript:;"
          class="iconfont icon-close-new"
          @click="cancel"
        ></a>
      </div>
      <div class="body">
        <i class="iconfont icon-warning"></i>
        <span>{{ text }}</span>
      </div>
      <div class="footer">
        <XtxButton size="mini" type="gray" @click="cancel">取消</XtxButton>
        <XtxButton size="mini" type="primary" @click="confirm">确认</XtxButton>
      </div>
    </div>
  </div>
</template>

<style scoped lang="less">
.xtx-confirm {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  z-index: 8888;
  background: rgba(0, 0, 0, 0);
  &.show {
    transition: all 0.5s;
    background: rgba(0, 0, 0, 0.5);
  }
  .wrapper {
    width: 400px;
    background: #fff;
    border-radius: 4px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -80%);
    opacity: 0;
    &.show {
      transition: all 0.5s;
      transform: translate(-50%, -50%);
      opacity: 1;
    }
    .header,
    .footer {
      height: 50px;
      line-height: 50px;
      padding: 0 20px;
    }
    .body {
      padding: 20px 40px;
      font-size: 16px;
      .icon-warning {
        color: @priceColor;
        margin-right: 3px;
        font-size: 16px;
      }
    }
    .footer {
      text-align: right;
      .xtx-button {
        margin-left: 20px;
      }
    }
    .header {
      position: relative;
      h3 {
        font-weight: normal;
        font-size: 18px;
      }
      a {
        position: absolute;
        right: 15px;
        top: 15px;
        font-size: 20px;
        width: 20px;
        height: 20px;
        line-height: 20px;
        text-align: center;
        color: #999;
        &:hover {
          color: #666;
        }
      }
    }
  }
}
</style>
